package com.coco.service.impl;

import com.coco.config.JwtProperties;
import com.coco.dao.UserMapper;
import com.coco.entity.UserInfo;
import com.coco.model.RecordModel;
import com.coco.model.UserInfoCondition;
import com.coco.service.UserService;
import com.coco.utils.CoPageResp;
import com.coco.utils.CookieUtils;
import com.coco.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author GHOSTLaycoo
 * @date 2020/2/28 0028 - 下午 16:16
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtProperties jwtProperties;

    @Override
    public String selectByUserInfo(String username, String password) {
        UserInfo user = userMapper.selectByUserInfo(username, password);
        if(user==null){
            return null;
        }

        try {
            UserInfo userInfo = new UserInfo();
            userInfo.setId(user.getId());
            userInfo.setName(user.getName());
            return JwtUtils.generateToken(userInfo,jwtProperties.getPrivateKey(),jwtProperties.getExpire());
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public CoPageResp<UserInfo> getUserByCondition(UserInfoCondition user) {
        CoPageResp<UserInfo> result = null;
        int newIndex = (user.getPageIndex()-1)*user.getPageSize();
        user.setPageIndex(newIndex);
        List<UserInfo> userByCondition = userMapper.getUserByCondition(user);
        long countByCondition = userMapper.getCountByCondition(user);
        if(!CollectionUtils.isEmpty(userByCondition) && userByCondition.size()>0){
            result = new CoPageResp<>();
            result.setData(userByCondition);
            result.setTotal(countByCondition);
        }
        return result;
    }

    @Override
    public int save(UserInfo user) {
        String uuid = UUID.randomUUID().toString();
        String userId = UUID.randomUUID().toString();
        user.setId(uuid);
        user.setUserId(userId);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        user.setDStatus("0");
        user.setLStatus("0");
        int save = userMapper.save(user);
        if(save>0){
            user.setUserId(userId);
            long time = new Date().getTime();
            int year = new Date(time).getYear();
            Date birthday = user.getBirthday();
            long time1 = birthday.getTime();
            int year1 = new Date(time1).getYear();
            int age = year-year1;
            user.setAge(String.valueOf(age));
            int i = userMapper.save1(user);
            return i;
        }
        return 0;
    }

    @Override
    public int update(UserInfo user) {
        user.setUpdateTime(new Date());
        int update = userMapper.update(user);
        if(update>0){
            int i = userMapper.update1(user);
            return i;
        }
        return 0;
    }

    @Override
    public int delete(String id) {
        UserInfo userById = userMapper.getUserById(id);
        int delete = userMapper.delete(id);
        if(delete>0){
            int i = userMapper.delete1(userById.getUserId());
            return i;
        }
        return 0;
    }

    @Override
    public int updateStatus(String id, String status, HttpServletRequest request) {
        if(id.equals("logOut")){
            String cookieValue = CookieUtils.getCookieValue(request, this.jwtProperties.getCookieName());
            try {
                UserInfo infoFromToken = JwtUtils.getInfoFromToken(cookieValue, jwtProperties.getPublicKey());
                id = infoFromToken.getId();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return userMapper.updateStatus(id,status);
    }

    @Override
    public UserInfo getUserInfoById(String id) {
        return userMapper.getUserById(id);
    }

}
